package net.jxtz.jxtzos.spider.process;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.jxtz.jxtzos.entity.spider.SpiderLogServe;
import net.jxtz.jxtzos.entity.spider.SpiderXinjianHousing;
import net.jxtz.jxtzos.mapper.redis.RedisMapper;
import net.jxtz.jxtzos.service.spider.SpiderXinjianHousingService;
import net.jxtz.jxtzos.spider.BaseSpiderProcessor;
import net.jxtz.jxtzos.spider.SpiderDefinition;
import net.jxtz.jxtzos.spider.SpiderFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author lmh
 * @version 1.0
 * @date 2021-09-14 09:41
 */

@Component
@Slf4j
public class XinJianInSaleHousingProcessor implements BaseSpiderProcessor {

    private final CloseableHttpClient httpClient = HttpClients.createDefault();

    public final static String SPIDER_NAME = "xinJianInSaleHousing";

    public final static String CACHE_NAME = "cache.spider.process.XinJianInSaleHousingProcessor";

    private final static int MAX_PAGE = 50;

    private final static int SECOND_SLEEP_TIME = 1;

    private final RedisMapper redisMapper;

    private final SpiderXinjianHousingService spiderXinjianInSaleHousingService;

    private Long spiderLogId = null;

    private XinJianHousingInformationProcessor xinJianHousingInformationProcessor;

    @Autowired
    public void setXinJianHousingInformationProcessor(XinJianHousingInformationProcessor xinJianHousingInformationProcessor) {
        this.xinJianHousingInformationProcessor = xinJianHousingInformationProcessor;
    }

    public XinJianInSaleHousingProcessor(RedisMapper redisMapper, SpiderXinjianHousingService spiderXinjianInSaleHousingService) {
        this.redisMapper = redisMapper;
        this.spiderXinjianInSaleHousingService = spiderXinjianInSaleHousingService;
        SpiderFactory instance = SpiderFactory.getInstance();
        instance.putSpiderDefinition(SPIDER_NAME, new SpiderDefinition("新建政府在售信息", SPIDER_NAME));
        instance.putSpiderProcess(SPIDER_NAME, this);
    }

    public boolean process(int page) {
        String url = "http://59.53.216.196/zt/House/ListCanSell?page=" + page + "&Place=&Project=";
        String s = httpClientInvoke(url);
        if (StringUtils.isBlank(s)) {
            return false;
        }
        Document indexDocument = Jsoup.parse(s);
        Element table = indexDocument.getElementsByTag("table").get(0).getElementsByTag("tbody").get(0);
        if (table == null) {
            return false;
        }
        Elements tr = table.getElementsByTag("tr");
        List<SpiderXinjianHousing> list = new ArrayList<>();
        for (Element element : tr) {
            SpiderXinjianHousing xinjianInSaleHousing = new SpiderXinjianHousing();
            String devCompany = element.getElementsByTag("td").get(0).text();
            Element projectName = element.getElementsByTag("td").get(1).getElementsByTag("a").get(0);
            String projectAreaName = element.getElementsByTag("td").get(2).text();
//            String inSaleHousing = element.getElementsByTag("td").get(3).text();
//            String inSaleOther = element.getElementsByTag("td").get(4).text();
//            String inSaleArea = element.getElementsByTag("td").get(5).text();
            xinjianInSaleHousing.setDevCompany(devCompany);
            xinjianInSaleHousing.setProjectName(projectName.text());
            xinjianInSaleHousing.setProjectAreaName(projectAreaName);
//            xinjianInSaleHousing.setInSaleHousing(inSaleHousing);
//            xinjianInSaleHousing.setInSaleOther(inSaleOther);
//            xinjianInSaleHousing.setInSaleArea(inSaleArea);
            if (spiderLogId != null) {
                xinjianInSaleHousing.setSpiderLogId(spiderLogId);
            }

            String href = projectName.attr("href");
            String details = XinJianHousingInformationProcessor.URL_PREFIX + "/" + href;
            String saleStatus = "可售";
            xinJianHousingInformationProcessor.detailsProcess(details, xinjianInSaleHousing, list, saleStatus);
        }

        saveCache(list);

        return true;
    }

    @SneakyThrows
    public String httpClientInvoke(String url) {
//        TimeUnit.SECONDS.sleep(SECOND_SLEEP_TIME);
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        httpGet.setHeader("Host", "59.53.216.196");
        httpGet.setHeader("Referer", url);
        httpGet.setHeader("Upgrade-Insecure-Requests", "1");
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36");
        CloseableHttpResponse response = httpClient.execute(httpGet);
        return EntityUtils.toString(response.getEntity());
    }

    private void saveCache(List<SpiderXinjianHousing> list) {
        for (SpiderXinjianHousing xinjianInSaleHousing : list) {
            redisMapper.leftPush(CACHE_NAME, xinjianInSaleHousing);
        }
    }

    @Override
    public void start(SpiderLogServe spiderLogServe, SpiderDefinition spiderDefinition) {
        int startCount = spiderXinjianInSaleHousingService.count();
        spiderLogId = spiderLogServe.getId();
        int page = 0;
        boolean flag = true;
        while (flag) {
            log.info("当前：" + page + "页");
            flag = process(page++);
            if (page >= MAX_PAGE) {
                flag = false;
            }
        }

        int endCount = spiderXinjianInSaleHousingService.count();

        int count = endCount - startCount;
        log.info("爬取完毕，爬取总量：" + count);
        spiderLogServe.setCount(count);
    }
}
